#!/bin/bash -e

function info() {
    echo Usage: `basename $0` [-p out_prefix -s out_suffix] bam_list
    exit 1
}

while getopts ":p:s:t:i:" opt; do
    case $opt in
        p) out_prefix=$OPTARG;;
        s) out_suffix=$OPTARG;;
        t) threads=$OPTARG;;
        i) interval=$OPTARG;;
        ?) info;;
    esac
done
shift $(($OPTIND - 1))

if [ $# -lt 1 ]; then info; fi

. $var || :

test -n "$interval" && interval="-L $interval"


# The PoN allows additional filtering of calls, e.g. those that arise from technical artifacts. Therefore, it is important that the PoN consist of samples that are technically similar to and representative of the tumor samples, e.g. that were sequenced on the same platform using the same chemistry and analyzed using the same toolchain.

for i in `cat $1`; do
    j=`basename $i .bam`
    echo $i
    echo $j
    $java_run/gatk38 \
    -T MuTect2 \
    -R $ref_genome $interval \
    -I:tumor $i \
    --cosmic $cosmic \
    -nct $nct \
    --artifact_detection_mode \
    -o $out_prefix.$j.normal_for_pon.vcf.gz

done

    # --dbsnp $dbsnp \


echo `find . -name "$out_prefix.*.normal_for_pon.vcf.gz" |sed 's/^/-V /'` > $out_prefix.normal_for_pon.list

echo combine samples
$java_run/gatk38 \
-T CombineVariants \
-R $ref_genome \
--arg_file $out_prefix.normal_for_pon.list \
-minN 2 \
--setKey "null" \
-nct $nct \
--filteredAreUncalled \
--filteredrecordsmergetype KEEP_IF_ANY_UNFILTERED \
-o $out_prefix.pon.vcf.gz

# The --genotypemergeoption UNIQUIFY allows us to use the same normal VCF twice, as different samples


. $cmd_done

